<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image
            round
            class="avatar"
            :src="profileObj.photo"
            @click="imageClickFn"
          />
          <!-- file 选择框 -->
          <input
            type="file"
            ref="iptFile"
            v-show="false"
            accept="image/*"
            @change="onFileChange"
          />
        </template>
      </van-cell>
      <van-cell
        title="名称"
        is-link
        :value="profileObj.name"
        @click="nameClickFn"
      />
      <van-cell title="生日" is-link :value="profileObj.birthday" @click="birthdayClickFn"/>
    </van-cell-group>

    <!-- 姓名编辑弹窗 -->
    <van-dialog
      v-model="isShowName"
      title="标题"
      show-cancel-button
      :before-close="beforeCloseFn"
    >
      <input v-model="inputUserName" type="text" v-fofo />
    </van-dialog>

    <!-- 时间选择器 -->
    <van-popup v-model="isShowTime" position="bottom" :style="{height:'50%'}" round>
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="dateCancel"
        @confirm="updateBirthday"
      />
    </van-popup>
  </div>
</template>

<script>
import { userProfileAPI, getUserPhotoAPI, updateUserProfileAPI } from '@/api'
import Notify from '@/ui/Notify.js'
import { formatDate } from '@/utils/date.js'
export default {
  name: 'UserEdit',
  data () {
    return {
      profileObj: {}, // 用户个人资料
      isShowName: false, // 控制姓名编辑框的显示
      inputUserName: '', // 姓名编辑框的值
      minDate: new Date(1920, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(2021, 0, 17),
      isShowTime: false
    }
  },
  async created () {
    const res = await userProfileAPI()
    this.profileObj = res.data.data
  },
  methods: {
    //   文件选择改变事件
    async onFileChange (e) {
      if (e.target.files.length === 0) return
      const theFd = new FormData()
      theFd.append('photo', e.target.files[0])
      const res = await getUserPhotoAPI(theFd)
      this.profileObj.photo = res.data.data.photo
    },
    // 头像编辑
    imageClickFn () {
      this.$refs.iptFile.click()
    },
    // 姓名编辑
    nameClickFn () {
      this.isShowName = true
      this.inputUserName = this.profileObj.name
    },
    // 姓名编辑框关闭前的回调
    async beforeCloseFn (action, done) {
      if (action === 'confirm') {
        const reg = /^[a-zA-Z0-9\u4e00-\u9fa5]{1,7}$/
        if (reg.test(this.inputUserName)) {
          await updateUserProfileAPI({
            name: this.inputUserName
          })
          this.profileObj.name = this.inputUserName
          done()
        } else {
          done(false)
          Notify({ type: 'warning', message: '只能是1-7位中英数字组合' })
        }
      } else {
        done()
      }
    },

    // 编辑生日
    birthdayClickFn () {
      this.isShowTime = true
      this.currentDate = new Date(this.profileObj.birthday)
    },
    // 日期选择器-取消事件
    dateCancel () {
      this.isShowTime = false
    },
    // 更新生日
    async updateBirthday () {
      await updateUserProfileAPI({
        birthday: formatDate(this.currentDate)
      })
      this.isShowTime = false
      this.profileObj.birthday = formatDate(this.currentDate)
    }
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
.van-nav-bar {
  background-color: orange;
  /deep/.van-icon {
    color: white;
  }
}
/deep/ .van-dialog__content {
  text-align: center;
  input {
    padding: 0;
    outline: none;
    border: none;
    text-align: center;
  }
}
</style>
